package com.whales.lms.rms.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.whales.lms.rms.web.model.ScheduleVO;
import com.whales.shared.model.CommonVO;
import com.whales.shared.service.GenericService;
import com.whales.shared.util.CalendarUtil;

@Service
public class ScheduleService extends GenericService<ScheduleVO, ScheduleVO> {
	public void createReg(List<ScheduleVO> scheduleVOs) throws Exception{
		if(scheduleVOs != null){
			super.delete("schedule.deleteReg", new ScheduleVO());
			for (ScheduleVO scheduleVO : scheduleVOs) {
				super.create("schedule.createReg", scheduleVO);
			}
		}else{
			ScheduleVO scheduleVO = new ScheduleVO();
			for (int i = 0; i < 7; i++) {
				scheduleVO.setWeekDay(i);
				scheduleVO.setOnYn("Y");
				super.create("schedule.createReg", scheduleVO);
			}
		}
	}
	
	public void createTemp(List<ScheduleVO> scheduleVOs) throws Exception{
		commonDao.create("schedule.createTemp", scheduleVOs);
	}

	public void deleteTempById(List<ScheduleVO> scheduleVOs) throws Exception{
		for (ScheduleVO scheduleVO : scheduleVOs) {
			super.delete("schedule.deleteTempById", scheduleVO);
		}
	}
	
	public int deleteZonePcByPcId(CommonVO commonVO) throws Exception{
		return commonDao.delete("schedule.deleteZonePcByPcId", commonVO);
	}
	
	private boolean isHoliDay() throws Exception{
		ScheduleVO scheduleVO = new ScheduleVO();
		scheduleVO.setWeekDay(new CalendarUtil().getWeekByIndex() - 1);
		//정기 휴일 check
		if(super.findInfo("schedule.findRegByWeekDay", scheduleVO) != null){
			return true;
		}
		//임시 휴일 check
		scheduleVO.setDateDay(CalendarUtil.getDateFormat("yyyy-MM-dd"));
		if(super.findInfo("schedule.findTempByDateDay", scheduleVO) != null){
			return true;
		}
		return false;
	}
	
	public List<ScheduleVO> getTurnOnOffPcListByTime(ScheduleVO scheduleVO) throws Exception{
		//휴일 check
		if(this.isHoliDay()){
			return null;
		}
		scheduleVO.setWeekDay(new CalendarUtil().getWeekByIndex() - 1);
		return super.getList("schedule.getTurnOnOffPcListByTime", scheduleVO);
	}
	
	public List<ScheduleVO> getToLivePcListByTime() throws Exception{
		//휴일 check
		if(this.isHoliDay()){
			return new ArrayList<ScheduleVO>();
		}
		ScheduleVO scheduleVO = new ScheduleVO();
		scheduleVO.setStartTime(CalendarUtil.getDateFormat("HH:mm"));
		return super.getList("schedule.getToLivePcListByTime", scheduleVO);
	}
	
	public List<ScheduleVO> getToLivePcListByIp(String ip) throws Exception{
		//휴일 check
		if(this.isHoliDay()){
			return new ArrayList<ScheduleVO>();
		}
		ScheduleVO scheduleVO = new ScheduleVO();
		scheduleVO.setStartTime(CalendarUtil.getDateFormat("HH:mm"));
		scheduleVO.setIp(ip);
		return super.getList("schedule.getToLivePcListByIp", scheduleVO);
	}
	
	public List<ScheduleVO> getRegList(ScheduleVO scheduleVO) throws Exception{
		if(commonDao.findList("schedule.getRegList", scheduleVO).size() == 0){
			this.createReg(null);
		}
		return super.getList("schedule.getRegList", scheduleVO);
	}
	
	public List<ScheduleVO> getTempList(ScheduleVO scheduleVO) throws Exception{
		return super.getList("schedule.getTempList", scheduleVO);
	}
	
	public List<ScheduleVO> getZoneList(ScheduleVO scheduleVO) throws Exception{
		return super.getList("schedule.getZoneList", scheduleVO);
	}
	
	public List<ScheduleVO> getZonePcList(ScheduleVO scheduleVO) throws Exception{
		return super.getList("schedule.getZonePcList", scheduleVO);
	}
	
	public void updateZone(List<ScheduleVO> scheduleVOs) throws Exception{
		super.update("schedule.createZone", scheduleVOs);
	}
	
	public void updateZonePc(List<ScheduleVO> scheduleVOs) throws Exception{
		super.update("schedule.createZonePc", scheduleVOs);
	}
}
